//package com.test;
//
//import com.grcb.afe.sm2sdk.AfeUtils;
//import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
//import org.bouncycastle.crypto.params.ECPublicKeyParameters;
//import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
//
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//
//import java.nio.charset.StandardCharsets;
//
///**
// * @Author Francis Xufan Du
// * @Date 2021/10/22
// */
//
//public class GRCB_AFE_SM2_SDK_Sample {
//    //大前置公钥
//    static String afePubKeyStr = "04986ea6d5505c63173d001557d06216f07181376781f20b1805cd519770dd4196f4f8561ebb0540c91a61b3b3a40b9e645dbe448bb6db07efb1f1fdf487ac910f";
//    //大前置分配给调用方的应用密钥
//    static String appSecretStr = "0093e9228cae04d7d2c8fde04cc5125398acc7503936ce3f12daadcbc2469c76ae";
//    //密钥的椭圆加密参数形式
//    static ECPublicKeyParameters afePubKey = AfeUtils.getPublicKeyObject(afePubKeyStr);
//    static ECPrivateKeyParameters appSecret = AfeUtils.getPrivateKeyObject(appSecretStr);
//
//    public static void main(String[] args) throws Exception {
//        //模拟调用方发送请求报文至大前置AFE时的加签和加密操作
////        consumerToAfe();
////        //模拟大前置返回应答报文至调用方后调用方的解密操作
////        afeToConsumer();
//        consumerToAfeVerify();
//    }
//
//    //模拟调用方发送请求报文至大前置AFE时的加签和加密操作
//    public static void consumerToAfe() throws Exception{
//        System.out.println("AFE公钥:" + afePubKeyStr);
//        System.out.println("应用密钥:" + appSecretStr);
//        System.out.println("-----调用方发送报文至大前置AFE-----");
//        //原文
//        String src = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Message><ISYS><appid>AFETEST</appid><nonce>790009135899</nonce><timestamp>1634694457</timestamp><ServiceCode>55123456</ServiceCode><SceneCode>01</SceneCode><SvrNodeId>XXX</SvrNodeId><GloSno>GPNB2023011401061511882792</GloSno></ISYS><Req><biaozhiz>1</biaozhiz></Req></Message>";
//        System.out.println("调用报文原文:" + src);
//        //加签
//        String srcWithSign = consumerToAfeSign(appSecret,src);
//        //加密
//        String srcEncrypted = consumerToAfeEncrypt(afePubKey,srcWithSign);
//    }
//
//    //模拟大前置返回应答报文至调用方后调用方的解密操作
//    public static void afeToConsumer() throws Exception{
//
//        System.out.println("-----大前置AFE返回应答报文至调用方-----");
//        //原文
//        String srcOrig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Message><OSYS><ServiceCode>55123456</ServiceCode><SceneCode>01</SceneCode><SvrNodeId>XXX</SvrNodeId><GloSno>GPNB2023011401061511882792</GloSno><RetCode>000000</RetCode><RetMsg>交易成功</RetMeg></OSYS><Resp><test>2</test></Resp></Message>";
//        System.out.println("返回报文原文:" + srcOrig);
//        String src = "04b67d5aad2a6bab86f36175e33084d4de6aa4ec744234369de52911830badcb357fa6e9e10943620360e5ad87a4e97972a613a20f4b40da136229679f6d6a0ba7b49db6fcaf5cbd4f0da820f1b495052ca4c2a8d91a6634f595e4c2edd5204debd66c4010e337775133356c7dcc03a252247338c40ff6a3414641fb81abaae05e652559f237feee038795dd7f6a5ab60fc4e3c4c922b633350b1409b92a809ffd6e56090b4dab37230e67daec9d5a59a35e01b14752be224da3b7d2b76fea8079db08f34c00dd09ffe94fb6872a273a9225b6778b2418918005ce4950d911761f0a67f24de9c78a2d0073e07fd1d6d5c53d743bafe346a67cb974a8945628454eab7e397b1e04b92f66afbae4b8f174213f3cdbf7bf9dfe93c05407122fb95b97e6b88247b26d3c01c5f663c3355fba61e279e95692d375bf22f0639f11837ff837da83f02c4c8ddd66d93cf4e0fe3bb679f04789bb46bdbb20b479287cb00de00ae4958c01e608c85bad52337716971f5437480d3f5b59cd";
//        //解密
//        String srcDecrypted = consumerToAfeDecrypt(appSecret,src);
//
//        System.out.println("对比解密后报文与原文,比对结果:" + srcDecrypted.equals(srcOrig));
//    }
//
//    public static String consumerToAfeSign(ECPrivateKeyParameters appSecret,String src) throws Exception{
//        //加签
//        //参与签名的字段
//        String forSign = String.format(
//                "appid=%s&GloSno=%s&nonce=%s&ServiceCode=%s&timestamp=%s",
//                "AFETEST",
//                "GPNB2023011401061511882792",
//                "790009135899",
//                "55123456",
//                "1634694457"
//
//        );
//        System.out.println("用来签名的字段为：" + forSign);
//
//        System.out.println("--加签--");
//        //获取签名
//        String signString = getSM2Signature(appSecret,forSign);
//
//        System.out.println("签名结果:" + signString);
//
//        String srcWithSign = getSignatureTagHelper(src,signString);
//        System.out.println("加上签名字段后的报文原文:" + srcWithSign);
//        return srcWithSign;
//    }
//
//    public static String consumerToAfeEncrypt(ECPublicKeyParameters afePubKey, String srcWithSign) throws Exception{
//        //加密
//        System.out.println("--加密--");
//        System.out.println("加密前初始的原文:" + srcWithSign);
//        //调用SDK接口解密，第一个参数为ECPublicKeyParameters对象公钥，第二个参数为待加密的数据，返回String类型加密后的数据
//        String encryptedData = AfeUtils.encrypt(afePubKey,srcWithSign);
//        System.out.println("加密后密文:" + encryptedData);
//        return encryptedData;
//    }
//
//    public static String consumerToAfeDecrypt(ECPrivateKeyParameters afePrivateKey, String src) throws Exception{
//        //解密
//        System.out.println("解密前密文:" + src);
//        //调用SDK接口加密，第一个参数为ECPrivateKeyParameters对象密钥，第二个参数为待解密的数据，返回String类型HEX格式的解密后的数据
//        String srcDecryptHex = AfeUtils.decrypt(afePrivateKey,src);
//        System.out.println("解密后得到的原文Hex:" + srcDecryptHex);
//        //转换为String默认编码，ByteUtils.fromHexString()接口讲HEX String转化为byte[]数组
//        String srcDecrypt = new String(ByteUtils.fromHexString(srcDecryptHex));
//        System.out.println("解密后得到的原文:" + srcDecrypt);
//        return srcDecrypt;
//    }
//
//
//
//    public static String getContextHelper(String xml, String tag){
//        String cont = new String();
//        String patternStr = "<" + tag + ">(.*)</" + tag + ">";
//        Pattern p = Pattern.compile(patternStr);
//        Matcher matcher = p.matcher(xml);
//        while(matcher.find()){
//            cont = matcher.group(1);
//        }
//        return cont;
//    }
//
//    public static String getSignatureTagHelper(String src,String signature){
//        String XMLWithSignature = src.substring(0,53)
//                + "<signature>"
//                + signature
//                + "</signature>"
//                + src.substring(53);
//        //System.out.println(XMLWithSignature);
//        return XMLWithSignature;
//    }
//
//
//    public static String getSM2Signature(ECPrivateKeyParameters priKey, String sourceData) throws Exception{
//        //调用SDK接口加签，第一个参数为ECPrivateKeyParameters对象密钥，第二个参数为用来加签的数据
//        return AfeUtils.getSign(priKey, sourceData);
//    }
//
//    public static boolean consumerToAfeVerify() throws Exception{
//        String appSecretAfeStr = "042ded97f1409b17030d91f7dbf3b31c575761bbc7dcc547315fac858136f5598617320078345e333f290336d5e00b63f9e6005a785ddaa129276a13065514d55e";
//
//        ECPublicKeyParameters appSecretAfe = AfeUtils.getPublicKeyObject(appSecretAfeStr);
//        String forSign = "appid=123456789&&gloSno=GSCF2021110909580300014341&&nonce=118805621724&&serviceCode=xx2x2&&timestamp=1636423120";
//        String signature = "30440220309f0624369a712c4af35a99096cf4a8dfe7313a50ae12db895755fcd97ec96102205441dac6195b7eb31a918bc08eb021365b57f79f91b336c00a737a8f4cb51b2c";
//
//        System.out.println("--验签--");
//        System.out.println("用来签名的字符串:" + forSign);
//        System.out.println("报文中获取的签名:" + signature);
//        boolean bool = AfeUtils.verify(appSecretAfe,forSign,signature);
//        System.out.println("验签结果:" + bool);
//        System.out.println("--验签完成--");
//        return bool;
//    }
//}
